enyo.kind({
    name: "MyApps.GolfPlusTv",
    kind: enyo.VFlexBox,
    events: {
        onBack: "",
    },
    components: [    /**
     * service
     */
    {
        name: "getData",
        kind: "WebService",
        url: "http://golfplus.mobile2you.fr/videos.json",
        onSuccess: "gotData",
        onFailure: "gotDataFailure"
    },    /**
     * Layout
     */
    {
        name: "pane",
        kind: "Pane",
        flex: 1,
        components: [{
            kind: "VFlexBox",
            flex: 1,
            
            components: [{
                kind: "PageHeader",
                className: "enyo-header-dark",
                components: [{
                    kind: "IconButton",
                    onclick: "doBack",
                    icon: "images/menu-icon-back.png",
                    className: "enyo-button-dark"
                }, {
                    kind: "VFlexBox",
                    flex: 1,
                    align: "center",
                    components: [{
                        content: "Golf Plus TV"
                    }, ]
                }, ]
            }, {
                kind: "Scroller",
                flex: 1,
                className: "list",
                components: [{
                    name: "list",
                    kind: "VirtualRepeater",
                    flex: 1,
                    onSetupRow: "listSetupRow",
                    components: [{
                        kind: "Item",
                        layoutKind: "HFlexLayout",
                        components: [{
                            name: "photo",
                            kind: "Image",
                            className: "photo",
                        }, {
                            flex: 1,
                            name: "description",
                            className: "videotext",
                        }, ],
                        onclick: "listItemClick"
                    }]
                }, ]
            }, ]
        }, {
            name: "videodetail",
            kind: "MyApps.Video",
            onBack: "goBack",
            lazy: true,
        
        
        }],
    }],
    create: function(){
        this.inherited(arguments);
        this.data = [];
        this.$.getData.call();
    },
    gotData: function(inSender, inResponse){
        this.data = inResponse;
        this.$.pane.render();
        console.log("got Data ");
    },
    gotDataFailure: function(inSender, inResponse){
        console.log("got failure from getData");
    },
    listSetupRow: function(inSender, inRow){
        var f = this.data[inRow];
        if (f) {
            this.$.photo.setSrc(f.video.img)
            this.$.description.setContent(f.video.description);
            return true;
        }
    },
    
    listItemClick: function(inSender, inEvent){
        var row = this.data[inEvent.rowIndex];
        this.$.pane.selectViewByName("videodetail");
        
        var link = row.video.link.replace("www.youtube.com/watch?v=", "www.youtube.com/embed/");
        link = link.replace("youtu.be/", "www.youtube.com/embed/");
        
        this.$.videodetail.setUrl(link);
    },
    
    
    goBack: function(inSender, inEvent){
        this.$.pane.back(inEvent);
    },

});



